package com.xk857.simpleeffect.adapter;

import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.VibrationEffect;
import android.os.Vibrator;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.appcompat.content.res.AppCompatResources;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.google.gson.reflect.TypeToken;
import com.xk857.simpleeffect.R;
import com.xk857.simpleeffect.activity.UpdateTodoActivity;
import com.xk857.simpleeffect.db.PersonDataBase;
import com.xk857.simpleeffect.domain.Result;
import com.xk857.simpleeffect.domain.entity.Business;
import com.xk857.simpleeffect.utils.OkHttpUtils;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.function.Consumer;

import cn.hutool.core.date.LocalDateTimeUtil;

public class ListYesBusinessAdapter extends BaseAdapter {

    private final Context mContext;
    private final ArrayList<Business> businessList;
    private final Consumer<Boolean> consumer;
    private final MediaPlayer mMediaPlayer = new MediaPlayer();
    private final Vibrator vibrator;
    public static final int[] iconDraws = {R.drawable.todo0, R.drawable.todo1, R.drawable.todo2, R.drawable.todo3, R.drawable.todo4, R.drawable.todo5};


    public ListYesBusinessAdapter(Context context, ArrayList<Business> businessList, Consumer<Boolean> consumer) {
        this.mContext = context;
        this.businessList = businessList;
        this.consumer = consumer;
        this.vibrator = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE);
    }

    @Override
    public int getCount() {
        return businessList.size();
    }

    @Override
    public Object getItem(int position) {
        return businessList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return businessList.get(position).getId();
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        ViewHolder viewHolder;
        if (view == null) {
            view = LayoutInflater.from(mContext).inflate(R.layout.list_business_yes_item, null);
            viewHolder = new ViewHolder();
            viewHolder.iconView = view.findViewById(R.id.icon_view);
            viewHolder.titleView = view.findViewById(R.id.title_view);
            viewHolder.dataView = view.findViewById(R.id.data_view);
            viewHolder.checkBox = view.findViewById(R.id.checkBox);
            viewHolder.progressBar = view.findViewById(R.id.progress_bar);
            viewHolder.mainLayout = view.findViewById(R.id.main_layout);
            viewHolder.btnDelete = view.findViewById(R.id.btnDelete);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        int icon = iconDraws[Integer.parseInt(businessList.get(position).getIcon())];
        viewHolder.iconView.setImageDrawable(AppCompatResources.getDrawable(mContext, icon));
        viewHolder.titleView.setText(businessList.get(position).getTitle());


        viewHolder.dataView.setText(LocalDateTimeUtil.format(businessList.get(position).getFinishTime(), "HH:mm"));
        viewHolder.checkBox.setOnClickListener(v -> {
            // 声音
            playRingtone();
            // 震动
            playVibrate();
            viewHolder.progressBar.setVisibility(View.VISIBLE);
            viewHolder.checkBox.setVisibility(View.GONE);
            Type type = new TypeToken<Result<Boolean>>() {
            }.getType();
            String url = OkHttpUtils.BASE_URL + "/api/v1/business/cancel/" + businessList.get(position).getId();
            OkHttpUtils.getInstance().get(mContext, url, type, false, consumer);
        });

        // 删除
        viewHolder.btnDelete.setOnClickListener(v -> {
            playVibrate();//震动
            Type type = new TypeToken<Result<Boolean>>() {}.getType();
            String url = OkHttpUtils.BASE_URL + "/api/v1/business/" + businessList.get(position).getId();
            OkHttpUtils.getInstance().delete(mContext, url, type, null,false, consumer);
        });


        viewHolder.mainLayout.setOnClickListener(v -> {
            Intent intent = new Intent(mContext, UpdateTodoActivity.class);
            intent.putExtra("id", businessList.get(position).getId());
            mContext.startActivity(intent);
        });
        return view;
    }

    private void playVibrate() {
        if (PersonDataBase.isVibrate(mContext)) {
            // 定义震动时长数组
            final long[] pattern = {0, 200, 0, 0, 200};
            VibrationEffect effect = VibrationEffect.createWaveform(pattern, -1);
            vibrator.vibrate(effect);
        }
    }

    // 播放铃声方法
    private void playRingtone() {
        try {
            if (PersonDataBase.isPlay(mContext)) {
                if (mMediaPlayer.isPlaying()) {
                    mMediaPlayer.stop();
                }
                mMediaPlayer.reset();
                mMediaPlayer.setDataSource(mContext, Uri.parse("android.resource://" + mContext.getPackageName() + "/raw/popping"));
                mMediaPlayer.prepare();
                mMediaPlayer.start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static class ViewHolder {
        ImageView iconView;
        TextView titleView;
        TextView dataView;
        CheckBox checkBox;
        ProgressBar progressBar;
        ConstraintLayout mainLayout;
        Button btnDelete;
    }
}
